﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace ProcessManagercs
{
    public static class ProcessMgr
    {
        /// <summary>
        /// The process-specific access rights.
        /// </summary>
        [Flags]
        public enum ProcessAccess : uint
        {
            /// <summary>
            /// Required to terminate a process using TerminateProcess.
            /// </summary>
            Terminate = 0x1,
            /// <summary>
            /// Required to create a thread.
            /// </summary>
            CreateThread = 0x2,
            /// <summary>
            /// Undocumented.
            /// </summary>
            SetSessionId = 0x4,
            /// <summary>
            /// Required to perform an operation on the address space of a process (see VirtualProtectEx and WriteProcessMemory).
            /// </summary>
            VmOperation = 0x8,
            /// <summary>
            /// Required to read memory in a process using ReadProcessMemory.
            /// </summary>
            VmRead = 0x10,
            /// <summary>
            /// Required to write to memory in a process using WriteProcessMemory.
            /// </summary>
            VmWrite = 0x20,
            /// <summary>
            /// Required to duplicate a handle using DuplicateHandle.
            /// </summary>
            DupHandle = 0x40,
            /// <summary>
            /// Required to create a process.
            /// </summary>
            CreateProcess = 0x80,
            /// <summary>
            /// Required to set memory limits using SetProcessWorkingSetSize.
            /// </summary>
            SetQuota = 0x100,
            /// <summary>
            /// Required to set certain information about a process, such as its priority class (see SetPriorityClass).
            /// </summary>
            SetInformation = 0x200,
            /// <summary>
            /// Required to retrieve certain information about a process, such as its token, exit code, and priority class (see OpenProcessToken, GetExitCodeProcess, GetPriorityClass, and IsProcessInJob).
            /// </summary>
            QueryInformation = 0x400,
            /// <summary>
            /// Undocumented.
            /// </summary>
            SetPort = 0x800,
            /// <summary>
            /// Required to suspend or resume a process.
            /// </summary>
            SuspendResume = 0x800,
            /// <summary>
            /// Required to retrieve certain information about a process (see QueryFullProcessImageName). A handle that has the PROCESS_QUERY_INFORMATION access right is automatically granted PROCESS_QUERY_LIMITED_INFORMATION.
            /// </summary>
            QueryLimitedInformation = 0x1000,
            /// <summary>
            /// Required to wait for the process to terminate using the wait functions.
            /// </summary>
            Synchronize = 0x100000
        }
        [DllImport("ntdll.dll")]
        private static extern uint NtResumeProcess([In] IntPtr processHandle);
        [DllImport("ntdll.dll")]
        private static extern uint NtSuspendProcess([In] IntPtr processHandle);
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern IntPtr OpenProcess(
         ProcessAccess desiredAccess,
         bool inheritHandle,
         int processId);
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool CloseHandle([In] IntPtr handle);

        /// <summary>
        /// 挂起进程
        /// </summary>
        /// <param name="processId"></param>
        public static void SuspendProcess(int processId)
        {
            IntPtr hProc = IntPtr.Zero;
            try
            {
                // Gets the handle to the Process
                hProc = OpenProcess(ProcessAccess.SuspendResume, false, processId);
                if (hProc != IntPtr.Zero)
                    NtSuspendProcess(hProc);
            }
            finally
            {
                // Don't forget to close handle you created.
                if (hProc != IntPtr.Zero)
                    CloseHandle(hProc);
            }
        }
        /// <summary>
        /// 恢复进程
        /// </summary>
        /// <param name="processId"></param>
        public static void ResumeProcess(int processId)
        {
            IntPtr hProc = IntPtr.Zero;
            try
            {
                // Gets the handle to the Process
                hProc = OpenProcess(ProcessAccess.SuspendResume, false, processId);
                if (hProc != IntPtr.Zero)
                    NtResumeProcess(hProc);
            }
            finally
            {
                // Don't forget to close handle you created.
                if (hProc != IntPtr.Zero)
                    CloseHandle(hProc);
            }
        }
        /// <summary>
        /// 挂起进程
        /// </summary>
        /// <param name="processId"></param>
        public static void SuspendProcess(string processName)
        {
            Process[] localByName =Process.GetProcesses();
            foreach (Process item in localByName)
            {
                if(item.ProcessName.IndexOf(processName)!= -1)
                SuspendProcess(item.Id);
            }
        }
        /// <summary>
        /// 恢复进程
        /// </summary>
        /// <param name="processId"></param>
        public static void ResumeProcess(string processName)
        {
            Process[] localByName = Process.GetProcesses();
            foreach (Process item in localByName)
            {
                if (item.ProcessName.IndexOf(processName) != -1)
                    ResumeProcess(item.Id);
            }
        }
        private static object ListLock = new object();
        public static void PoolDealWith(string processName)
        {
            List<int> ProcessList = new List<int>();
            System.Threading.Thread GetID = new System.Threading.Thread(() =>
            {
                while (true)
                {
                    Process[] name = Process.GetProcesses();
                    List<int> idList = new List<int>();
                    foreach (Process item in name)
                    {
                        if (item.ProcessName.IndexOf(processName) != -1)
                        {
                            idList.Add(item.Id);
                        }
                    }
                    lock (ListLock)
                    {
                        ProcessList = idList.Distinct().ToList();
                    }
                    System.Threading.Thread.Sleep(6000);
                }
            });
            GetID.IsBackground = true;
            GetID.Start();
            System.Threading.Thread dowith = new System.Threading.Thread(() =>
            {
                while (true)
                {

                    foreach (var item in ProcessList)
                    {
                        SuspendProcess(item);
                    }
                    foreach (var item in ProcessList)
                    {
                        ResumeProcess(item);
                        System.Threading.Thread.Sleep(300);
                        SuspendProcess(item);
                    }
                    System.Threading.Thread.Sleep(1000);
                }

            });
            dowith.IsBackground = true;
            dowith.Start();
        }
    }
}
